package com.itzhihao.controller;

import com.itzhihao.common.constant.StoreStatus;
import com.itzhihao.entity.Store;
import com.itzhihao.service.IStoreService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/store")
public class StoreController extends BaseController {

    @Resource
    private IStoreService storeService;

    @PostMapping("/back/list")
    public TableDataInfo getBackList(@RequestBody PageDomain pageDomain,
                                     @RequestBody Store store){
        return storeService.getBackList(pageDomain,store);
    }

    @GetMapping("/own/detail")
    public AjaxResult getStoreDetail(Store store){
        Store storeDetail = storeService.getOwnStoreDetail();
        return success(storeDetail);
    }

    @PostMapping("/list")
    public TableDataInfo getList(@RequestBody PageDomain pageDomain,@RequestBody Store store){
        return storeService.getList(pageDomain,store);
    }

    /**
     * 查看地址详情
     * @param id
     * @return
     */
    @GetMapping
    public AjaxResult getDetail(@RequestParam(value = "store_id") Long id){
        Store store = storeService.getDetail(id);
        return success(store);
    }

    @PutMapping
    public AjaxResult update(@RequestBody Store store){
        boolean isUpdate = storeService.updateOrSave(store);
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }

    @PutMapping("/noPassAudit")
    public AjaxResult noPassAudit(@RequestBody Store store){
        boolean isUpdate = storeService.updateStatus(store, StoreStatus.NO_PASS_AUDIT.ordinal());
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }

    @PutMapping("/stoped")
    public AjaxResult stoped(@RequestBody Store store){
        boolean isUpdate = storeService.updateStatus(store, StoreStatus.STOPED.ordinal());
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }

    @PutMapping("/passAudit")
    public AjaxResult passAudit(@RequestBody Store store){
        boolean isUpdate = storeService.updateStatus(store, StoreStatus.PASS_AUDIT.ordinal());
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }

    @DeleteMapping("/delete/{store_id}")
    public AjaxResult delete(@PathVariable(value = "store_id") Long id){
        boolean isDelete = storeService.deleteById(id);
        if(isDelete){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败，请重试");
        }
    }

    @GetMapping("/selectByProdId")
    public AjaxResult selectByProdId(@RequestParam Long prodId){
        Store store = storeService.selectByProdId(prodId);
        return success(store);
    }

    /**
     * 新增
     * @param store
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody Store store){
        boolean isSave = storeService.updateOrSave(store);
        if (isSave){
            return AjaxResult.success("保存成功");
        }else{
            return error("保存失败,当前用户已注册过商店");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Delete("/delete/{ids}")
    public AjaxResult deleteBatch(@PathVariable String ids){
        Long[] idLongArr = Convert.toLongArray(ids);
        List<Long> idLongList = Arrays.asList(idLongArr);
        boolean isDelete = storeService.deleteByIds(idLongList);
        if(isDelete){
            return success("批量删除成功");
        }else{
            return error("批量删除失败");
        }
    }

    @GetMapping("/selectByUserId")
    public AjaxResult getByUserId(@RequestParam(value = "user_id")Long userId){
        Store store = storeService.selectByUserId(userId);
        return success(store);
    }

    @GetMapping("/own/hasStore")
    public AjaxResult hasOwnStore(){
        Boolean exist = storeService.hasOwnStore();
        return success(exist);
    }

    /**
     * 获取某个商店的商品总数
     * @param storeId
     * @return
     */
    @GetMapping("/prod/total")
    public AjaxResult getProductTotal(@RequestParam(value = "store_id")Long storeId){
        Long total = storeService.getProdTotal(storeId);
        return success(total);
    }
}
